package com.gopay.common.dts.manager.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.gopay.common.domain.dts.DtsBusinessActivityLog;
import com.gopay.common.dts.constant.DtsBALActivity;
import com.gopay.common.dts.constant.DtsBALDomain;
import com.gopay.common.dts.constant.DtsBALStatus;
import com.gopay.common.dts.dao.DtsBusinessActivityLogDAO;
import com.gopay.common.dts.manager.DtsBusinessActivityLogManager;

/**
 * TCC业务活动日指标，管理器实现
 * 
 * @author MA HUAN
 * 
 */

@Service(value = "dtsBusinessActivityLogManager")
public class DtsBusinessActivityLogManagerImpl implements
		DtsBusinessActivityLogManager {

	@Resource(name = "dtsBusinessActivityLogDAO")
	private DtsBusinessActivityLogDAO dtsBusinessActivityLogDAO;

	@Override
	public DtsBusinessActivityLog getByComp(DtsBALDomain domain,
			DtsBALActivity activity, String snapkey) {
		return dtsBusinessActivityLogDAO.getByComp(domain.getValue(),
				activity.getValue(), snapkey);
	}

	@Override
	public DtsBusinessActivityLog saveLog(DtsBALDomain domain,
			DtsBALActivity activity, String snapkey, String snapshot) {
		
		DtsBusinessActivityLog log = new DtsBusinessActivityLog();
		log.setDomain(domain.getValue());
		log.setActivity(activity.getValue());
		log.setSnapkey(snapkey);
		log.setStatus(DtsBALStatus.ACTIVE.getValue());
		log.setCreateTime(new Date());
		log.setShapshot(snapshot);
		return dtsBusinessActivityLogDAO.save(log);
	}

	@Override
	public DtsBusinessActivityLog updateLogStatus(DtsBALDomain domain,
			DtsBALActivity activity, String snapkey, DtsBALStatus status) {
		
		DtsBusinessActivityLog log = dtsBusinessActivityLogDAO.getByComp(domain.getValue(), activity.getValue(), snapkey);
		if(log != null) {
			log.setStatus(status.getValue());
			log.setFinishTime(new Date());
			
			dtsBusinessActivityLogDAO.update(log);
			return log;
		}
		return null;
	}

	@Override
	public void updateLog(DtsBusinessActivityLog log) {
		dtsBusinessActivityLogDAO.update(log);
	}
}
